/*
 * @lc app=leetcode.cn id=682 lang=javascript
 *
 * [682] 棒球比赛
 */

// @lc code=start
/**
 * @param {string[]} ops
 * @return {number}
 */
var calPoints = function(ops) {
   const stack = [];
   for(let opt of ops) {
       if (opt === 'C') {
           stack.pop();
       } else if (opt === 'D') {
           const val = stack.pop()
           stack.push(val, val * 2);
       } else if (opt === '+') {
           const val1 = stack.pop();
           const val2 = stack.pop();
           stack.push(val2, val1, val1 + val2);
       } else {
           stack.push(Number(opt));
       }
   }
   return stack.reduce((total, val) => total + val, 0);
};
// @lc code=end

